#include "widget.h"
#include "./ui_widget.h"
#include <QMessageBox>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    
    // 连接信号槽
    connect(ui->loginButton, &QPushButton::clicked, this, &Widget::onLoginButtonClicked);
    connect(ui->exitButton, &QPushButton::clicked, this, &Widget::onExitButtonClicked);
    
    // 设置窗口居中显示
    setWindowTitle("用户登录系统");
    
    // 设置密码输入框的回车键触发登录
    connect(ui->passwordEdit, &QLineEdit::returnPressed, this, &Widget::onLoginButtonClicked);
}

Widget::~Widget()
{
    delete ui;
}

void Widget::onLoginButtonClicked()
{
    QString username = ui->usernameEdit->text().trimmed();
    QString password = ui->passwordEdit->text();
    
    // 验证用户名和密码
    if (username == "USER" && password == "123456") {
        QMessageBox::information(this, "登录成功", "欢迎使用系统！", QMessageBox::Ok);
        // 登录成功后可以在这里添加其他操作，比如打开主界面
    } else {
        QMessageBox::warning(this, "登录失败", "用户名或密码错误！", QMessageBox::Ok);
        // 清空密码输入框
        ui->passwordEdit->clear();
        ui->passwordEdit->setFocus();
    }
}

void Widget::onExitButtonClicked()
{
    // 确认是否退出
    QMessageBox::StandardButton reply = QMessageBox::question(this, "确认退出", 
                                                             "确定要退出程序吗？", 
                                                             QMessageBox::Yes | QMessageBox::No);
    if (reply == QMessageBox::Yes) {
        close();
    }
}
